﻿Imports System.Linq
Imports System.Data.SqlClient
Imports System.Collections.Generic
Imports SISSAT.BusinessLayer.DomainModels
Imports SISSAT.DataAcessLayer.StatementSources
Imports SISSAT.DataAcessLayer.PersistentStorage
Imports SISSAT.BusinessLayer.Utilities

Namespace Services
    Public Class CourseService
        Inherits ServiceBase
        Implements ICourseService

        Public Sub Create(name As String, description As String, code As String, createdBy As Integer) Implements ICourseService.Create
            StatementSource = New CreateCourseStatementSource(name, description, code, createdBy)
            Dim nonQuery As Integer = DbHelper.ExecuteNonQuery(StatementSource)
        End Sub

        Public Sub Delete(courseId As Integer) Implements ICourseService.Delete
            StatementSource = New DeleteCourseStatementSource(courseId)
            Dim nonQuery As Integer = DbHelper.ExecuteNonQuery(StatementSource)
        End Sub

        Public Sub Edit(entity As Course, modifiedBy As Integer) Implements ICourseService.Edit
            StatementSource = New UpdateCourseStatementSource(entity.CourseId, entity.Name, entity.Description, entity.Code, modifiedBy)
            Dim nonQuery As Integer = DbHelper.ExecuteNonQuery(StatementSource)
        End Sub

        Public Function ReadAll() As IEnumerable(Of Course) Implements ICourseService.ReadAll
            StatementSource = New ReadAllCourseStatementSource()
            Dim dataReader As SqlDataReader = DbHelper.ExecuteReader(StatementSource)

            Return DataMapper.MapDataToEntity(Of Course)(dataReader)
        End Function

        Public Function ReadById(courseId As Integer) As Course Implements ICourseService.ReadById
            StatementSource = New ReadByCourseIdStatementSource(courseId)
            Dim dataReader As SqlDataReader = DbHelper.ExecuteReader(StatementSource)

            Return DataMapper.MapDataToEntity(Of Course)(dataReader).FirstOrDefault()
        End Function
    End Class
End Namespace

